﻿using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RMS.Common
{
    public class NLogHelper
    {
        public Logger MyLogger = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 处理异步方法的错误日志
        /// </summary>
        /// <param name="t">异步事务</param>
        /// <param name="message">错误说明</param>
        public void TaskLog(Task t, string message = null)
        {
            Task ttEnd = t.ContinueWith((task) =>
            {
                if (task.Exception != null)
                {
                    if (!string.IsNullOrWhiteSpace(message))
                        MyLogger.Error(message);
                    MyLogger.Fatal(task.Exception);
                }
            }, TaskContinuationOptions.OnlyOnFaulted);
        }
        /// <summary>
        /// 记录普通日志
        /// </summary>
        /// <param name="message">日志信息</param>
        public void Info(string message)
        {
            MyLogger.Info(message);
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="e">错误</param>
        public void Error(Exception e)
        {
            MyLogger.Error(e);
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">详情</param>
        public void Error(string message)
        {
            MyLogger.Error(message);
        }
        /// <summary>
        /// 记录致命错误日志
        /// </summary>
        /// <param name="message">详情</param>
        public void Fatal(string message)
        {
            MyLogger.Fatal(message);
        }
        /// <summary>
        /// 记录致命错误日志
        /// </summary>
        /// <param name="e">错误</param>
        public void Fatal(Exception e)
        {
            MyLogger.Fatal(e);
        }

    }
}
